** FOR 2014 PANEL AND LATER YEARS

** Assemble information on household structure, by month.  Create variables indicating whether
** household includes:
**		- any children under age 19
**		- any parents/ any mothers of those kids
** 		- any parents of those parents (and hence grandparents of the children)
**********************************************************************************************



**************
** WAVE 1
**************

** First just extract identifying variables and sex, to help in figuring out
** sex of parent 1 and sex of parent 2
use ssuid monthcode pnum esex using pu2014w1.dta
rename pnum epnpar1
rename esex sex_par1
save Sex_Par1, replace
clear

use ssuid monthcode pnum esex using pu2014w1.dta
rename pnum epnpar2
rename esex sex_par2
save Sex_Par2, replace
clear

**********************************************************************************************
** Now extract variables for main part of this family data assembly


use ssuid pnum spanel swave monthcode tage ems epnpar1 epnpar2 esex renroll eedftpt epnspouse rmesr eeduc erace wpfinwgt erelrp tehc_st tpearn thtotinc using pu2014w1.dta

** Merge in info on sex of each parent
merge m:1 ssuid monthcode epnpar1 using Sex_Par1
keep if _merge~=2
drop _merge


** Merge in info on sex of each parent
merge m:1 ssuid monthcode epnpar2 using Sex_Par2
keep if _merge~=2
drop _merge

** Make epnmom, setting it equal to epnpar1 if parent 1 is female, replacing it
** with epnpar2 if (and only if) epnpar1 is male.  Repeat same idea to make epndad
gen epnmom = .
replace epnmom = epnpar1 if sex_par1==2
replace epnmom = epnpar2 if sex_par1==1 & sex_par2==2

gen epndad = .
replace epndad = epnpar1 if sex_par1==1
replace epndad = epnpar2 if sex_par1==2 & sex_par2==1



** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with their mothers or fathers

gen Under19 = (tage<19 & (ems~=1 & ems~=2))
gen HaveMominHH = (epnmom~=.)
gen HaveDadinHH = (epndad~=.)

gen Under19_LiveWithMom = (Under19==1 & HaveMominHH==1)
gen Under19_LiveWithDad = (Under19==1 & HaveDadinHH==1)
gen Under19_LiveWithParent = (Under19_LiveWithMom==1 | Under19_LiveWithDad==1)


** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & (renroll==1 | eedftpt==1) & (ems~=1 & ems~=2))
gen Under24FTS_LiveWithMom = (Under24FTS==1 & HaveMominHH==1)
gen Under24FTS_LiveWithDad = (Under24FTS==1 & HaveDadinHH==1)
gen Under24FTS_LiveWithParent = (Under24FTS==1 & (HaveMominHH==1 | HaveDadinHH==1))


gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)
gen PotentialDep_LiveWithMom = (Under19_LiveWithMom==1 | Under24FTS_LiveWithMom==1)
gen PotentialDep_LiveWithDad = (Under19_LiveWithDad==1 | Under24FTS_LiveWithDad==1)

** Count up number of such kids per household per month. 
sort ssuid monthcode
by ssuid monthcode: egen N_PotentialDep = total(PotentialDep)
by ssuid monthcode: egen N_PotentialDep_LiveWithMom = total(PotentialDep_LiveWithMom)
by ssuid monthcode: egen N_PotentialDep_LiveWithDad = total(PotentialDep_LiveWithDad)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort ssuid monthcode
by ssuid monthcode: egen MinAge_PotentialDep = min(temp)
drop temp


** On the kid-under-19 records, record the person number of the person 
** who is the listed mom of that under-19 kid. 
gen epn_mom=epnmom if PotentialDep_LiveWithMom==1


** Count up number of distinct moms-of-under-19 kids
egen distinctmomtag = tag(ssuid monthcode epn_mom)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctMoms = total(distinctmomtag)


** At household-month level, record person numbers of up to 5 people
** identified as moms of an under-19 kid living with parent
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom1 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom1
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom2 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom2
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom3 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom3
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom4 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom4
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom5 = min(epn_mom)


** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenMom=(pnum==temp_epn_mom1 | pnum==temp_epn_mom2 | pnum==temp_epn_mom3 | pnum==temp_epn_mom4 | pnum==temp_epn_mom4)


** Repeat for dads: count up number of distinct identified dads in household,
** at HH level record person numbers of up to 4 people identified as dads of 
** under-19 kids 
gen epn_dad=epndad if PotentialDep_LiveWithDad==1

egen distinctdadtag = tag(ssuid monthcode epn_dad)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctDads = total(distinctdadtag)

gen N_DistinctParents = N_DistinctMoms + N_DistinctDads


** At household-month level, record person numbers of up to 4 people
** identified as dads of an under-19 kid living with parent
sort ssuid monthcode epn_dad
by ssuid monthcode: egen temp_epn_dad1 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad1
sort ssuid monthcode epn_dad
by ssuid monthcode: egen temp_epn_dad2 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad2
sort ssuid monthcode epn_dad
by ssuid monthcode: egen temp_epn_dad3 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad3
sort ssuid monthcode epn_dad
by ssuid monthcode: egen temp_epn_dad4 = min(epn_dad)


** Generate an indicator, at the individual level, for being a dad of under-19 kid
gen PotentialMidGenDad=(pnum==temp_epn_dad1 | pnum==temp_epn_dad2 | pnum==temp_epn_dad3 | pnum==temp_epn_dad4)

gen PotentialMidGenParent=(PotentialMidGenMom==1 | PotentialMidGenDad==1)


** Drop intermediate variables that were used in the identification process
drop temp_epn_mom1 temp_epn_mom2 temp_epn_mom3 temp_epn_mom4 temp_epn_dad1 temp_epn_dad2 temp_epn_dad3 temp_epn_dad4



** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen ParentHasMom = (PotentialMidGenParent==1 & epnmom~=. )


** Count up how many potential-mid-gen-parents in the HH have their own moms in HH
sort ssuid monthcode
by ssuid monthcode: egen N_Parent_LiveWithMom = total(ParentHasMom)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gmom=epnmom if ParentHasMom==1

** Count up number of distinct moms-of-parents
egen distinctgmomtag = tag(ssuid monthcode epn_gmom)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctGMom = total(distinctgmomtag)


** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid monthcode epn_gmom N_DistinctGMom
by ssuid monthcode: egen temp_epn_gmom1 = min(epn_gmom)

replace epn_gmom=. if epn_gmom==temp_epn_gmom1
sort ssuid monthcode epn_gmom
by ssuid monthcode: egen temp_epn_gmom2 = min(epn_gmom)



** Now identify if the PotentialMidGenParent has his/her own father in HH
gen ParentHasDad = (PotentialMidGenParent==1 & epndad~=. )

gen ParentHasParent = (ParentHasMom==1 | ParentHasDad==1)

** Count up how many potential-mid-gen-parents in the HH have their own dads in HH
sort ssuid monthcode
by ssuid monthcode: egen N_Parent_LiveWithDad = total(ParentHasDad)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed dad of that mid-gen parent 
gen epn_gdad=epndad if ParentHasDad==1

** Count up number of distinct parents-of-parents
egen distinctgdadtag = tag(ssuid monthcode epn_gdad)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctGDad = total(distinctgdadtag)

gen N_DistinctGP = N_DistinctGMom + N_DistinctGDad


** At household-month level, record person numbers of up to 2 people 
** identified as dads of a parent
sort ssuid monthcode epn_gdad
by ssuid monthcode: egen temp_epn_gdad1 = min(epn_gdad)

replace epn_gdad=. if epn_gdad==temp_epn_gdad1
sort ssuid monthcode epn_gdad
by ssuid monthcode: egen temp_epn_gdad2 = min(epn_gdad)


** Generate an indicator, at the individual level, for being a mom or dad of a parent (a grandparent)

gen PotentialOldGenParent=(pnum==temp_epn_gmom1 | pnum==temp_epn_gmom2 | pnum==temp_epn_gdad1 | pnum==temp_epn_gdad2)



** Identify spouses of these grandparents
gen temp_epn_spouse_gm1 = epnspous if pnum==temp_epn_gmom1
sort ssuid monthcode
by ssuid monthcode: egen epn_spouse_gm1 = min(temp_epn_spouse_gm1)
gen SpouseOfGM1=(pnum==epn_spouse_gm1)
replace PotentialOldGenParent=1 if SpouseOfGM1==1

gen temp_epn_spouse_gm2 = epnspous if pnum==temp_epn_gmom2
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_gm2 = min(temp_epn_spouse_gm2)
gen SpouseOfGM2=(pnum==epn_spouse_gm2)
replace PotentialOldGenParent=1 if SpouseOfGM2==1

gen temp_epn_spouse_gd1 = epnspous if pnum==temp_epn_gdad1
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_gd1 = min(temp_epn_spouse_gd1)
gen SpouseOfGD1=(pnum==epn_spouse_gd1)
replace PotentialOldGenParent=1 if SpouseOfGD1==1

gen temp_epn_spouse_gd2 = epnspous if pnum==temp_epn_gdad2
sort ssuid monthcode
by ssuid monthcode: egen epn_spouse_gd2 = min(temp_epn_spouse_gd2)
gen SpouseOfGD2=(pnum==epn_spouse_gd2)
replace PotentialOldGenParent=1 if SpouseOfGD2==1

gen PotentialOldGenMom = ((pnum==temp_epn_gmom1 | pnum==temp_epn_gmom2)  & esex==2)


** Make other demographic variables

gen Employed = (rmesr==1 | rmesr==2 | rmesr==3 | rmesr==4 | rmesr==5)

** Note that education coding is a bit different in 2014 than in earlier years
gen HSGrad = (eeduc==39 )
gen SomeCollege = (eeduc==40 | eeduc==41 | eeduc==42)
gen CollegeGrad = (eeduc==43 | eeduc==44 | eeduc==45 | eeduc==46 )

gen Female = (esex==2)

gen White = (erace==1)

gen Married = (ems==1 | ems==2)

gen FTStudent = (renroll==1 | eedftpt==1)

save TempData, replace


*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep ssuid swave monthcode pnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep  wpfinwgt erelrp tehc_st tpearn thtotinc FTStudent PotentialDep
destring tehc_st, gen(tfipsst)
duplicates drop

save MidGenParentData2014_w1, replace
clear


*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1

keep ssuid swave monthcode pnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep wpfinwgt erelrp tehc_st tpearn thtotinc 
destring tehc_st, gen(tfipsst)
duplicates drop

save OldGenParentData2014_w1, replace
clear



********************************************************************************
** Identify cases of adults who do NOT have potential child 
** deps living with them, but who do have "elderly" parents living with them.


use TempData
gen PotentialEmptyNester = (PotentialDep==0 & PotentialMidGenParent==0 & tage>24)

** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen EmptyNesterHasMom = (PotentialEmptyNester==1 & epnmom~=. )


** On the potential-mid-gen-EN-parents records, record the person number of the person 
** who is the listed mom of that mid-gen empty nester parent 
gen epn_engmom=epnmom if EmptyNesterHasMom==1


** Count up number of distinct moms-of-parents
egen distinctengmomtag = tag(ssuid epn_engmom)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctENGMom = total(distinctengmomtag)



** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid monthcode epn_engmom
by ssuid monthcode: egen temp_epn_engmom1 = min(epn_engmom)

replace epn_engmom=. if epn_engmom==temp_epn_gmom1
sort ssuid monthcode epn_engmom
by ssuid monthcode: egen temp_epn_engmom2 = min(epn_engmom)


** Now identify if the PotentialMidGenENParent has his/her own father in HH
gen EmptyNesterHasDad = (PotentialEmptyNester==1 & epndad~=. )

gen EmptyNesterHasParent = (EmptyNesterHasMom==1 | EmptyNesterHasDad==1)


** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed dad of that mid-gen parent 
gen epn_engdad=epndad if EmptyNesterHasDad==1


** Count up number of distinct parents-of-parents
egen distinctengdadtag = tag(ssuid monthcode epn_engdad)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctENGDad = total(distinctengdadtag)

gen N_DistinctENGP = N_DistinctENGMom + N_DistinctENGDad

** At household-month level, record person numbers of up to 2 people 
** identified as dads of a parent
sort ssuid monthcode epn_engdad
by ssuid monthcode: egen temp_epn_engdad1 = min(epn_engdad)

replace epn_engdad=. if epn_gdad==temp_epn_engdad1
sort ssuid monthcode epn_engdad
by ssuid monthcode: egen temp_epn_engdad2 = min(epn_engdad)



** Generate an indicator, at the individual level, for being a parent of a potential empty nester
** And then generate one for being a female empty nest grandparent

gen PotentialEmptyNestGP=(pnum==temp_epn_engmom1 | pnum==temp_epn_engmom2 | pnum==temp_epn_engdad1 | pnum==temp_epn_engdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_engm1 = epnspous if pnum==temp_epn_engmom1
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_engm1 = min(temp_epn_spouse_engm1)
gen SpouseOfENGM1=(pnum==epn_spouse_engm1)
replace PotentialEmptyNestGP=1 if SpouseOfENGM1==1

gen temp_epn_spouse_engm2 = epnspous if pnum==temp_epn_engmom2
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_engm2 = min(temp_epn_spouse_engm2)
gen SpouseOfENGM2=(pnum==epn_spouse_engm2)
replace PotentialEmptyNestGP=1 if SpouseOfENGM2==1

gen temp_epn_spouse_engd1 = epnspous if pnum==temp_epn_engdad1
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_engd1 = min(temp_epn_spouse_engd1)
gen SpouseOfENGD1=(pnum==epn_spouse_engd1)
replace PotentialEmptyNestGP=1 if SpouseOfENGD1==1

gen temp_epn_spouse_engd2 = epnspous if pnum==temp_epn_engdad2
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_engd2 = min(temp_epn_spouse_engd2)
gen SpouseOfENGD2=(pnum==epn_spouse_engd2)
replace PotentialEmptyNestGP=1 if SpouseOfENGD2==1

gen PotentialEmptyNestGP_Female = (PotentialEmptyNestGP==1 & esex==2)


save Temp2Data, replace




******************************************************************
** Keep data on potential empty-nester parents
keep if PotentialEmptyNester==1 & PotentialEmptyNestGP~=1

destring tehc_st, gen(tfipsst)

keep ssuid monthcode pnum PotentialEmptyNester tage Employed HSGrad SomeCollege CollegeGrad Female White Married EmptyNesterHasParent N_DistinctENGP  wpfinwgt erelrp tfipsst tpearn thtotinc 

duplicates drop

save EmptyNesterData2014_w1, replace
clear




*******************************************************************************
** Keep and save just the data on potential grandparents in households where
** mid-gen parent is empty-nester
use Temp2Data
destring tehc_st, gen(tfipsst)


keep if PotentialEmptyNestGP==1

keep ssuid monthcode pnum PotentialEmptyNestGP PotentialEmptyNestGP_Female tage Employed HSGrad SomeCollege CollegeGrad Female White Married   wpfinwgt erelrp tfipsst tpearn thtotinc 

duplicates drop

save EmptyNesterGPData2014_w1, replace
clear





**************
** WAVE 2
**************


** First just extract identifying variables and sex, to help in figuring out
** sex of parent 1 and sex of parent 2
use ssuid monthcode pnum esex using pu2014w2.dta
rename pnum epnpar1
rename esex sex_par1
save Sex_Par1_2014_w2, replace
clear

use ssuid monthcode pnum esex using pu2014w2.dta
rename pnum epnpar2
rename esex sex_par2
save Sex_Par2_2014_w2, replace
clear

**********************************************************************************************
** Now extract variables for main part of this family data assembly


use ssuid pnum spanel swave monthcode tage ems epnpar1 epnpar2 esex renroll eedftpt epnspouse rmesr eeduc erace wpfinwgt erelrp tehc_st tpearn thtotinc using pu2014w2.dta

** Merge in info on sex of each parent
merge m:1 ssuid monthcode epnpar1 using Sex_Par1_2014_w2
keep if _merge~=2
drop _merge


** Merge in info on sex of each parent
merge m:1 ssuid monthcode epnpar2 using Sex_Par2_2014_w2
keep if _merge~=2
drop _merge

** Make epnmom, setting it equal to epnpar1 if parent 1 is female, replacing it
** with epnpar2 if (and only if) epnpar1 is male.  Repeat same idea to make epndad
gen epnmom = .
replace epnmom = epnpar1 if sex_par1==2
replace epnmom = epnpar2 if sex_par1==1 & sex_par2==2

gen epndad = .
replace epndad = epnpar1 if sex_par1==1
replace epndad = epnpar2 if sex_par1==2 & sex_par2==1



** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with their mothers or fathers

gen Under19 = (tage<19 & (ems~=1 & ems~=2))
gen HaveMominHH = (epnmom~=.)
gen HaveDadinHH = (epndad~=.)

gen Under19_LiveWithMom = (Under19==1 & HaveMominHH==1)
gen Under19_LiveWithDad = (Under19==1 & HaveDadinHH==1)
gen Under19_LiveWithParent = (Under19_LiveWithMom==1 | Under19_LiveWithDad==1)


** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & (renroll==1 | eedftpt==1) & (ems~=1 & ems~=2))
gen Under24FTS_LiveWithMom = (Under24FTS==1 & HaveMominHH==1)
gen Under24FTS_LiveWithDad = (Under24FTS==1 & HaveDadinHH==1)
gen Under24FTS_LiveWithParent = (Under24FTS==1 & (HaveMominHH==1 | HaveDadinHH==1))


gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)
gen PotentialDep_LiveWithMom = (Under19_LiveWithMom==1 | Under24FTS_LiveWithMom==1)
gen PotentialDep_LiveWithDad = (Under19_LiveWithDad==1 | Under24FTS_LiveWithDad==1)

** Count up number of such kids per household per month. 
sort ssuid monthcode
by ssuid monthcode: egen N_PotentialDep = total(PotentialDep)
by ssuid monthcode: egen N_PotentialDep_LiveWithMom = total(PotentialDep_LiveWithMom)
by ssuid monthcode: egen N_PotentialDep_LiveWithDad = total(PotentialDep_LiveWithDad)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort ssuid monthcode
by ssuid monthcode: egen MinAge_PotentialDep = min(temp)
drop temp


** On the kid-under-19 records, record the person number of the person 
** who is the listed mom of that under-19 kid. 
gen epn_mom=epnmom if PotentialDep_LiveWithMom==1


** Count up number of distinct moms-of-under-19 kids
egen distinctmomtag = tag(ssuid monthcode epn_mom)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctMoms = total(distinctmomtag)


** At household-month level, record person numbers of up to 5 people
** identified as moms of an under-19 kid living with parent
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom1 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom1
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom2 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom2
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom3 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom3
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom4 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom4
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom5 = min(epn_mom)


** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenMom=(pnum==temp_epn_mom1 | pnum==temp_epn_mom2 | pnum==temp_epn_mom3 | pnum==temp_epn_mom4 | pnum==temp_epn_mom4)


** Repeat for dads: count up number of distinct identified dads in household,
** at HH level record person numbers of up to 4 people identified as dads of 
** under-19 kids 
gen epn_dad=epndad if PotentialDep_LiveWithDad==1

egen distinctdadtag = tag(ssuid monthcode epn_dad)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctDads = total(distinctdadtag)

gen N_DistinctParents = N_DistinctMoms + N_DistinctDads


** At household-month level, record person numbers of up to 4 people
** identified as dads of an under-19 kid living with parent
sort ssuid monthcode epn_dad
by ssuid monthcode: egen temp_epn_dad1 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad1
sort ssuid monthcode epn_dad
by ssuid monthcode: egen temp_epn_dad2 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad2
sort ssuid monthcode epn_dad
by ssuid monthcode: egen temp_epn_dad3 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad3
sort ssuid monthcode epn_dad
by ssuid monthcode: egen temp_epn_dad4 = min(epn_dad)


** Generate an indicator, at the individual level, for being a dad of under-19 kid
gen PotentialMidGenDad=(pnum==temp_epn_dad1 | pnum==temp_epn_dad2 | pnum==temp_epn_dad3 | pnum==temp_epn_dad4)

gen PotentialMidGenParent=(PotentialMidGenMom==1 | PotentialMidGenDad==1)


** Drop intermediate variables that were used in the identification process
drop temp_epn_mom1 temp_epn_mom2 temp_epn_mom3 temp_epn_mom4 temp_epn_dad1 temp_epn_dad2 temp_epn_dad3 temp_epn_dad4



** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen ParentHasMom = (PotentialMidGenParent==1 & epnmom~=. )


** Count up how many potential-mid-gen-parents in the HH have their own moms in HH
sort ssuid monthcode
by ssuid monthcode: egen N_Parent_LiveWithMom = total(ParentHasMom)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gmom=epnmom if ParentHasMom==1

** Count up number of distinct moms-of-parents
egen distinctgmomtag = tag(ssuid monthcode epn_gmom)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctGMom = total(distinctgmomtag)


** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid monthcode epn_gmom N_DistinctGMom
by ssuid monthcode: egen temp_epn_gmom1 = min(epn_gmom)

replace epn_gmom=. if epn_gmom==temp_epn_gmom1
sort ssuid monthcode epn_gmom
by ssuid monthcode: egen temp_epn_gmom2 = min(epn_gmom)



** Now identify if the PotentialMidGenParent has his/her own father in HH
gen ParentHasDad = (PotentialMidGenParent==1 & epndad~=. )

gen ParentHasParent = (ParentHasMom==1 | ParentHasDad==1)

** Count up how many potential-mid-gen-parents in the HH have their own dads in HH
sort ssuid monthcode
by ssuid monthcode: egen N_Parent_LiveWithDad = total(ParentHasDad)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed dad of that mid-gen parent 
gen epn_gdad=epndad if ParentHasDad==1

** Count up number of distinct parents-of-parents
egen distinctgdadtag = tag(ssuid monthcode epn_gdad)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctGDad = total(distinctgdadtag)

gen N_DistinctGP = N_DistinctGMom + N_DistinctGDad


** At household-month level, record person numbers of up to 2 people 
** identified as dads of a parent
sort ssuid monthcode epn_gdad
by ssuid monthcode: egen temp_epn_gdad1 = min(epn_gdad)

replace epn_gdad=. if epn_gdad==temp_epn_gdad1
sort ssuid monthcode epn_gdad
by ssuid monthcode: egen temp_epn_gdad2 = min(epn_gdad)


** Generate an indicator, at the individual level, for being a mom or dad of a parent (a grandparent)

gen PotentialOldGenParent=(pnum==temp_epn_gmom1 | pnum==temp_epn_gmom2 | pnum==temp_epn_gdad1 | pnum==temp_epn_gdad2)



** Identify spouses of these grandparents
gen temp_epn_spouse_gm1 = epnspous if pnum==temp_epn_gmom1
sort ssuid monthcode
by ssuid monthcode: egen epn_spouse_gm1 = min(temp_epn_spouse_gm1)
gen SpouseOfGM1=(pnum==epn_spouse_gm1)
replace PotentialOldGenParent=1 if SpouseOfGM1==1

gen temp_epn_spouse_gm2 = epnspous if pnum==temp_epn_gmom2
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_gm2 = min(temp_epn_spouse_gm2)
gen SpouseOfGM2=(pnum==epn_spouse_gm2)
replace PotentialOldGenParent=1 if SpouseOfGM2==1

gen temp_epn_spouse_gd1 = epnspous if pnum==temp_epn_gdad1
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_gd1 = min(temp_epn_spouse_gd1)
gen SpouseOfGD1=(pnum==epn_spouse_gd1)
replace PotentialOldGenParent=1 if SpouseOfGD1==1

gen temp_epn_spouse_gd2 = epnspous if pnum==temp_epn_gdad2
sort ssuid monthcode
by ssuid monthcode: egen epn_spouse_gd2 = min(temp_epn_spouse_gd2)
gen SpouseOfGD2=(pnum==epn_spouse_gd2)
replace PotentialOldGenParent=1 if SpouseOfGD2==1

gen PotentialOldGenMom = ((pnum==temp_epn_gmom1 | pnum==temp_epn_gmom2)  & esex==2)


** Make other demographic variables

gen Employed = (rmesr==1 | rmesr==2 | rmesr==3 | rmesr==4 | rmesr==5)

** Note that education coding is a bit different in 2014 than in earlier years
gen HSGrad = (eeduc==39 )
gen SomeCollege = (eeduc==40 | eeduc==41 | eeduc==42)
gen CollegeGrad = (eeduc==43 | eeduc==44 | eeduc==45 | eeduc==46 )

gen Female = (esex==2)

gen White = (erace==1)

gen Married = (ems==1 | ems==2)

gen FTStudent = (renroll==1 | eedftpt==1)

save TempData, replace


*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep ssuid swave monthcode pnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep  wpfinwgt erelrp tehc_st tpearn thtotinc FTStudent PotentialDep
destring tehc_st, gen(tfipsst)
duplicates drop

save MidGenParentData2014_w2, replace
clear


*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1

keep ssuid swave monthcode pnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep wpfinwgt erelrp tehc_st tpearn thtotinc 
destring tehc_st, gen(tfipsst)
duplicates drop

save OldGenParentData2014_w2, replace
clear



********************************************************************************
** Identify cases of adults who do NOT have potential child 
** deps living with them, but who do have "elderly" parents living with them.


use TempData
gen PotentialEmptyNester = (PotentialDep==0 & PotentialMidGenParent==0 & tage>24)

** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen EmptyNesterHasMom = (PotentialEmptyNester==1 & epnmom~=. )


** On the potential-mid-gen-EN-parents records, record the person number of the person 
** who is the listed mom of that mid-gen empty nester parent 
gen epn_engmom=epnmom if EmptyNesterHasMom==1


** Count up number of distinct moms-of-parents
egen distinctengmomtag = tag(ssuid epn_engmom)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctENGMom = total(distinctengmomtag)



** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid monthcode epn_engmom
by ssuid monthcode: egen temp_epn_engmom1 = min(epn_engmom)

replace epn_engmom=. if epn_engmom==temp_epn_gmom1
sort ssuid monthcode epn_engmom
by ssuid monthcode: egen temp_epn_engmom2 = min(epn_engmom)


** Now identify if the PotentialMidGenENParent has his/her own father in HH
gen EmptyNesterHasDad = (PotentialEmptyNester==1 & epndad~=. )

gen EmptyNesterHasParent = (EmptyNesterHasMom==1 | EmptyNesterHasDad==1)


** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed dad of that mid-gen parent 
gen epn_engdad=epndad if EmptyNesterHasDad==1


** Count up number of distinct parents-of-parents
egen distinctengdadtag = tag(ssuid monthcode epn_engdad)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctENGDad = total(distinctengdadtag)

gen N_DistinctENGP = N_DistinctENGMom + N_DistinctENGDad

** At household-month level, record person numbers of up to 2 people 
** identified as dads of a parent
sort ssuid monthcode epn_engdad
by ssuid monthcode: egen temp_epn_engdad1 = min(epn_engdad)

replace epn_engdad=. if epn_gdad==temp_epn_engdad1
sort ssuid monthcode epn_engdad
by ssuid monthcode: egen temp_epn_engdad2 = min(epn_engdad)



** Generate an indicator, at the individual level, for being a parent of a potential empty nester
** And then generate one for being a female empty nest grandparent

gen PotentialEmptyNestGP=(pnum==temp_epn_engmom1 | pnum==temp_epn_engmom2 | pnum==temp_epn_engdad1 | pnum==temp_epn_engdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_engm1 = epnspous if pnum==temp_epn_engmom1
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_engm1 = min(temp_epn_spouse_engm1)
gen SpouseOfENGM1=(pnum==epn_spouse_engm1)
replace PotentialEmptyNestGP=1 if SpouseOfENGM1==1

gen temp_epn_spouse_engm2 = epnspous if pnum==temp_epn_engmom2
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_engm2 = min(temp_epn_spouse_engm2)
gen SpouseOfENGM2=(pnum==epn_spouse_engm2)
replace PotentialEmptyNestGP=1 if SpouseOfENGM2==1

gen temp_epn_spouse_engd1 = epnspous if pnum==temp_epn_engdad1
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_engd1 = min(temp_epn_spouse_engd1)
gen SpouseOfENGD1=(pnum==epn_spouse_engd1)
replace PotentialEmptyNestGP=1 if SpouseOfENGD1==1

gen temp_epn_spouse_engd2 = epnspous if pnum==temp_epn_engdad2
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_engd2 = min(temp_epn_spouse_engd2)
gen SpouseOfENGD2=(pnum==epn_spouse_engd2)
replace PotentialEmptyNestGP=1 if SpouseOfENGD2==1

gen PotentialEmptyNestGP_Female = (PotentialEmptyNestGP==1 & esex==2)


save Temp2Data, replace




******************************************************************
** Keep data on potential empty-nester parents
keep if PotentialEmptyNester==1 & PotentialEmptyNestGP~=1

destring tehc_st, gen(tfipsst)

keep ssuid monthcode pnum PotentialEmptyNester tage Employed HSGrad SomeCollege CollegeGrad Female White Married EmptyNesterHasParent N_DistinctENGP  wpfinwgt erelrp tfipsst tpearn thtotinc 

duplicates drop

save EmptyNesterData2014_w2, replace
clear




*******************************************************************************
** Keep and save just the data on potential grandparents in households where
** mid-gen parent is empty-nester
use Temp2Data
destring tehc_st, gen(tfipsst)


keep if PotentialEmptyNestGP==1

keep ssuid monthcode pnum PotentialEmptyNestGP PotentialEmptyNestGP_Female tage Employed HSGrad SomeCollege CollegeGrad Female White Married   wpfinwgt erelrp tfipsst tpearn thtotinc 

duplicates drop

save EmptyNesterGPData2014_w2, replace
clear





**************
** WAVE 3
**************


** First just extract identifying variables and sex, to help in figuring out
** sex of parent 1 and sex of parent 2
use ssuid monthcode pnum esex using pu2014w3_13.dta
rename pnum epnpar1
rename esex sex_par1
save Sex_Par1_2014_w3, replace
clear

use ssuid monthcode pnum esex using pu2014w3_13.dta
rename pnum epnpar2
rename esex sex_par2
save Sex_Par2_2014_w3, replace
clear

**********************************************************************************************
** Now extract variables for main part of this family data assembly


use ssuid pnum spanel swave monthcode tage ems epnpar1 epnpar2 esex renroll eedftpt epnspouse rmesr eeduc erace wpfinwgt erelrp tehc_st tpearn thtotinc using pu2014w3_13.dta

** Merge in info on sex of each parent
merge m:1 ssuid monthcode epnpar1 using Sex_Par1_2014_w3
keep if _merge~=2
drop _merge


** Merge in info on sex of each parent
merge m:1 ssuid monthcode epnpar2 using Sex_Par2_2014_w3
keep if _merge~=2
drop _merge

** Make epnmom, setting it equal to epnpar1 if parent 1 is female, replacing it
** with epnpar2 if (and only if) epnpar1 is male.  Repeat same idea to make epndad
gen epnmom = .
replace epnmom = epnpar1 if sex_par1==2
replace epnmom = epnpar2 if sex_par1==1 & sex_par2==2

gen epndad = .
replace epndad = epnpar1 if sex_par1==1
replace epndad = epnpar2 if sex_par1==2 & sex_par2==1



** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with their mothers or fathers

gen Under19 = (tage<19 & (ems~=1 & ems~=2))
gen HaveMominHH = (epnmom~=.)
gen HaveDadinHH = (epndad~=.)

gen Under19_LiveWithMom = (Under19==1 & HaveMominHH==1)
gen Under19_LiveWithDad = (Under19==1 & HaveDadinHH==1)
gen Under19_LiveWithParent = (Under19_LiveWithMom==1 | Under19_LiveWithDad==1)


** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & (renroll==1 | eedftpt==1) & (ems~=1 & ems~=2))
gen Under24FTS_LiveWithMom = (Under24FTS==1 & HaveMominHH==1)
gen Under24FTS_LiveWithDad = (Under24FTS==1 & HaveDadinHH==1)
gen Under24FTS_LiveWithParent = (Under24FTS==1 & (HaveMominHH==1 | HaveDadinHH==1))


gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)
gen PotentialDep_LiveWithMom = (Under19_LiveWithMom==1 | Under24FTS_LiveWithMom==1)
gen PotentialDep_LiveWithDad = (Under19_LiveWithDad==1 | Under24FTS_LiveWithDad==1)

** Count up number of such kids per household per month. 
sort ssuid monthcode
by ssuid monthcode: egen N_PotentialDep = total(PotentialDep)
by ssuid monthcode: egen N_PotentialDep_LiveWithMom = total(PotentialDep_LiveWithMom)
by ssuid monthcode: egen N_PotentialDep_LiveWithDad = total(PotentialDep_LiveWithDad)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort ssuid monthcode
by ssuid monthcode: egen MinAge_PotentialDep = min(temp)
drop temp


** On the kid-under-19 records, record the person number of the person 
** who is the listed mom of that under-19 kid. 
gen epn_mom=epnmom if PotentialDep_LiveWithMom==1


** Count up number of distinct moms-of-under-19 kids
egen distinctmomtag = tag(ssuid monthcode epn_mom)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctMoms = total(distinctmomtag)


** At household-month level, record person numbers of up to 5 people
** identified as moms of an under-19 kid living with parent
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom1 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom1
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom2 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom2
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom3 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom3
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom4 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom4
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom5 = min(epn_mom)


** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenMom=(pnum==temp_epn_mom1 | pnum==temp_epn_mom2 | pnum==temp_epn_mom3 | pnum==temp_epn_mom4 | pnum==temp_epn_mom4)


** Repeat for dads: count up number of distinct identified dads in household,
** at HH level record person numbers of up to 4 people identified as dads of 
** under-19 kids 
gen epn_dad=epndad if PotentialDep_LiveWithDad==1

egen distinctdadtag = tag(ssuid monthcode epn_dad)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctDads = total(distinctdadtag)

gen N_DistinctParents = N_DistinctMoms + N_DistinctDads


** At household-month level, record person numbers of up to 4 people
** identified as dads of an under-19 kid living with parent
sort ssuid monthcode epn_dad
by ssuid monthcode: egen temp_epn_dad1 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad1
sort ssuid monthcode epn_dad
by ssuid monthcode: egen temp_epn_dad2 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad2
sort ssuid monthcode epn_dad
by ssuid monthcode: egen temp_epn_dad3 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad3
sort ssuid monthcode epn_dad
by ssuid monthcode: egen temp_epn_dad4 = min(epn_dad)


** Generate an indicator, at the individual level, for being a dad of under-19 kid
gen PotentialMidGenDad=(pnum==temp_epn_dad1 | pnum==temp_epn_dad2 | pnum==temp_epn_dad3 | pnum==temp_epn_dad4)

gen PotentialMidGenParent=(PotentialMidGenMom==1 | PotentialMidGenDad==1)


** Drop intermediate variables that were used in the identification process
drop temp_epn_mom1 temp_epn_mom2 temp_epn_mom3 temp_epn_mom4 temp_epn_dad1 temp_epn_dad2 temp_epn_dad3 temp_epn_dad4



** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen ParentHasMom = (PotentialMidGenParent==1 & epnmom~=. )


** Count up how many potential-mid-gen-parents in the HH have their own moms in HH
sort ssuid monthcode
by ssuid monthcode: egen N_Parent_LiveWithMom = total(ParentHasMom)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gmom=epnmom if ParentHasMom==1

** Count up number of distinct moms-of-parents
egen distinctgmomtag = tag(ssuid monthcode epn_gmom)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctGMom = total(distinctgmomtag)


** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid monthcode epn_gmom N_DistinctGMom
by ssuid monthcode: egen temp_epn_gmom1 = min(epn_gmom)

replace epn_gmom=. if epn_gmom==temp_epn_gmom1
sort ssuid monthcode epn_gmom
by ssuid monthcode: egen temp_epn_gmom2 = min(epn_gmom)



** Now identify if the PotentialMidGenParent has his/her own father in HH
gen ParentHasDad = (PotentialMidGenParent==1 & epndad~=. )

gen ParentHasParent = (ParentHasMom==1 | ParentHasDad==1)

** Count up how many potential-mid-gen-parents in the HH have their own dads in HH
sort ssuid monthcode
by ssuid monthcode: egen N_Parent_LiveWithDad = total(ParentHasDad)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed dad of that mid-gen parent 
gen epn_gdad=epndad if ParentHasDad==1

** Count up number of distinct parents-of-parents
egen distinctgdadtag = tag(ssuid monthcode epn_gdad)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctGDad = total(distinctgdadtag)

gen N_DistinctGP = N_DistinctGMom + N_DistinctGDad


** At household-month level, record person numbers of up to 2 people 
** identified as dads of a parent
sort ssuid monthcode epn_gdad
by ssuid monthcode: egen temp_epn_gdad1 = min(epn_gdad)

replace epn_gdad=. if epn_gdad==temp_epn_gdad1
sort ssuid monthcode epn_gdad
by ssuid monthcode: egen temp_epn_gdad2 = min(epn_gdad)


** Generate an indicator, at the individual level, for being a mom or dad of a parent (a grandparent)

gen PotentialOldGenParent=(pnum==temp_epn_gmom1 | pnum==temp_epn_gmom2 | pnum==temp_epn_gdad1 | pnum==temp_epn_gdad2)



** Identify spouses of these grandparents
gen temp_epn_spouse_gm1 = epnspous if pnum==temp_epn_gmom1
sort ssuid monthcode
by ssuid monthcode: egen epn_spouse_gm1 = min(temp_epn_spouse_gm1)
gen SpouseOfGM1=(pnum==epn_spouse_gm1)
replace PotentialOldGenParent=1 if SpouseOfGM1==1

gen temp_epn_spouse_gm2 = epnspous if pnum==temp_epn_gmom2
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_gm2 = min(temp_epn_spouse_gm2)
gen SpouseOfGM2=(pnum==epn_spouse_gm2)
replace PotentialOldGenParent=1 if SpouseOfGM2==1

gen temp_epn_spouse_gd1 = epnspous if pnum==temp_epn_gdad1
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_gd1 = min(temp_epn_spouse_gd1)
gen SpouseOfGD1=(pnum==epn_spouse_gd1)
replace PotentialOldGenParent=1 if SpouseOfGD1==1

gen temp_epn_spouse_gd2 = epnspous if pnum==temp_epn_gdad2
sort ssuid monthcode
by ssuid monthcode: egen epn_spouse_gd2 = min(temp_epn_spouse_gd2)
gen SpouseOfGD2=(pnum==epn_spouse_gd2)
replace PotentialOldGenParent=1 if SpouseOfGD2==1

gen PotentialOldGenMom = ((pnum==temp_epn_gmom1 | pnum==temp_epn_gmom2)  & esex==2)


** Make other demographic variables

gen Employed = (rmesr==1 | rmesr==2 | rmesr==3 | rmesr==4 | rmesr==5)

** Note that education coding is a bit different in 2014 than in earlier years
gen HSGrad = (eeduc==39 )
gen SomeCollege = (eeduc==40 | eeduc==41 | eeduc==42)
gen CollegeGrad = (eeduc==43 | eeduc==44 | eeduc==45 | eeduc==46 )

gen Female = (esex==2)

gen White = (erace==1)

gen Married = (ems==1 | ems==2)

gen FTStudent = (renroll==1 | eedftpt==1)

save TempData, replace


*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep ssuid swave monthcode pnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep  wpfinwgt erelrp tehc_st tpearn thtotinc FTStudent PotentialDep
destring tehc_st, gen(tfipsst)
duplicates drop

save MidGenParentData2014_w3, replace
clear


*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1

keep ssuid swave monthcode pnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep wpfinwgt erelrp tehc_st tpearn thtotinc 
destring tehc_st, gen(tfipsst)
duplicates drop

save OldGenParentData2014_w3, replace
clear



********************************************************************************
** Identify cases of adults who do NOT have potential child 
** deps living with them, but who do have "elderly" parents living with them.


use TempData
gen PotentialEmptyNester = (PotentialDep==0 & PotentialMidGenParent==0 & tage>24)

** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen EmptyNesterHasMom = (PotentialEmptyNester==1 & epnmom~=. )


** On the potential-mid-gen-EN-parents records, record the person number of the person 
** who is the listed mom of that mid-gen empty nester parent 
gen epn_engmom=epnmom if EmptyNesterHasMom==1


** Count up number of distinct moms-of-parents
egen distinctengmomtag = tag(ssuid epn_engmom)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctENGMom = total(distinctengmomtag)



** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid monthcode epn_engmom
by ssuid monthcode: egen temp_epn_engmom1 = min(epn_engmom)

replace epn_engmom=. if epn_engmom==temp_epn_gmom1
sort ssuid monthcode epn_engmom
by ssuid monthcode: egen temp_epn_engmom2 = min(epn_engmom)


** Now identify if the PotentialMidGenENParent has his/her own father in HH
gen EmptyNesterHasDad = (PotentialEmptyNester==1 & epndad~=. )

gen EmptyNesterHasParent = (EmptyNesterHasMom==1 | EmptyNesterHasDad==1)


** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed dad of that mid-gen parent 
gen epn_engdad=epndad if EmptyNesterHasDad==1


** Count up number of distinct parents-of-parents
egen distinctengdadtag = tag(ssuid monthcode epn_engdad)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctENGDad = total(distinctengdadtag)

gen N_DistinctENGP = N_DistinctENGMom + N_DistinctENGDad

** At household-month level, record person numbers of up to 2 people 
** identified as dads of a parent
sort ssuid monthcode epn_engdad
by ssuid monthcode: egen temp_epn_engdad1 = min(epn_engdad)

replace epn_engdad=. if epn_gdad==temp_epn_engdad1
sort ssuid monthcode epn_engdad
by ssuid monthcode: egen temp_epn_engdad2 = min(epn_engdad)



** Generate an indicator, at the individual level, for being a parent of a potential empty nester
** And then generate one for being a female empty nest grandparent

gen PotentialEmptyNestGP=(pnum==temp_epn_engmom1 | pnum==temp_epn_engmom2 | pnum==temp_epn_engdad1 | pnum==temp_epn_engdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_engm1 = epnspous if pnum==temp_epn_engmom1
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_engm1 = min(temp_epn_spouse_engm1)
gen SpouseOfENGM1=(pnum==epn_spouse_engm1)
replace PotentialEmptyNestGP=1 if SpouseOfENGM1==1

gen temp_epn_spouse_engm2 = epnspous if pnum==temp_epn_engmom2
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_engm2 = min(temp_epn_spouse_engm2)
gen SpouseOfENGM2=(pnum==epn_spouse_engm2)
replace PotentialEmptyNestGP=1 if SpouseOfENGM2==1

gen temp_epn_spouse_engd1 = epnspous if pnum==temp_epn_engdad1
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_engd1 = min(temp_epn_spouse_engd1)
gen SpouseOfENGD1=(pnum==epn_spouse_engd1)
replace PotentialEmptyNestGP=1 if SpouseOfENGD1==1

gen temp_epn_spouse_engd2 = epnspous if pnum==temp_epn_engdad2
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_engd2 = min(temp_epn_spouse_engd2)
gen SpouseOfENGD2=(pnum==epn_spouse_engd2)
replace PotentialEmptyNestGP=1 if SpouseOfENGD2==1

gen PotentialEmptyNestGP_Female = (PotentialEmptyNestGP==1 & esex==2)


save Temp2Data, replace




******************************************************************
** Keep data on potential empty-nester parents
keep if PotentialEmptyNester==1 & PotentialEmptyNestGP~=1

destring tehc_st, gen(tfipsst)

keep ssuid monthcode pnum PotentialEmptyNester tage Employed HSGrad SomeCollege CollegeGrad Female White Married EmptyNesterHasParent N_DistinctENGP  wpfinwgt erelrp tfipsst tpearn thtotinc 

duplicates drop

save EmptyNesterData2014_w3, replace
clear




*******************************************************************************
** Keep and save just the data on potential grandparents in households where
** mid-gen parent is empty-nester
use Temp2Data
destring tehc_st, gen(tfipsst)


keep if PotentialEmptyNestGP==1

keep ssuid monthcode pnum PotentialEmptyNestGP PotentialEmptyNestGP_Female tage Employed HSGrad SomeCollege CollegeGrad Female White Married   wpfinwgt erelrp tfipsst tpearn thtotinc 

duplicates drop

save EmptyNesterGPData2014_w3, replace
clear






**************
** WAVE 4
**************


** First just extract identifying variables and sex, to help in figuring out
** sex of parent 1 and sex of parent 2
use ssuid monthcode pnum esex using pu2014w4.dta
rename pnum epnpar1
rename esex sex_par1
save Sex_Par1_2014_w4, replace
clear

use ssuid monthcode pnum esex using pu2014w4.dta
rename pnum epnpar2
rename esex sex_par2
save Sex_Par2_2014_w4, replace
clear

**********************************************************************************************
** Now extract variables for main part of this family data assembly


use ssuid pnum spanel swave monthcode tage ems epnpar1 epnpar2 esex renroll eedftpt epnspouse rmesr eeduc erace wpfinwgt erelrp tehc_st tpearn thtotinc using pu2014w4.dta

** Merge in info on sex of each parent
merge m:1 ssuid monthcode epnpar1 using Sex_Par1_2014_w4
keep if _merge~=2
drop _merge


** Merge in info on sex of each parent
merge m:1 ssuid monthcode epnpar2 using Sex_Par2_2014_w4
keep if _merge~=2
drop _merge

** Make epnmom, setting it equal to epnpar1 if parent 1 is female, replacing it
** with epnpar2 if (and only if) epnpar1 is male.  Repeat same idea to make epndad
gen epnmom = .
replace epnmom = epnpar1 if sex_par1==2
replace epnmom = epnpar2 if sex_par1==1 & sex_par2==2

gen epndad = .
replace epndad = epnpar1 if sex_par1==1
replace epndad = epnpar2 if sex_par1==2 & sex_par2==1



** Identify kids who are under age 19 (hence age-eligible for EITC) and living
** with their mothers or fathers

gen Under19 = (tage<19 & (ems~=1 & ems~=2))
gen HaveMominHH = (epnmom~=.)
gen HaveDadinHH = (epndad~=.)

gen Under19_LiveWithMom = (Under19==1 & HaveMominHH==1)
gen Under19_LiveWithDad = (Under19==1 & HaveDadinHH==1)
gen Under19_LiveWithParent = (Under19_LiveWithMom==1 | Under19_LiveWithDad==1)


** Identify kids who are under age 24 and full time students
gen Under24FTS = (tage<24 & (renroll==1 | eedftpt==1) & (ems~=1 & ems~=2))
gen Under24FTS_LiveWithMom = (Under24FTS==1 & HaveMominHH==1)
gen Under24FTS_LiveWithDad = (Under24FTS==1 & HaveDadinHH==1)
gen Under24FTS_LiveWithParent = (Under24FTS==1 & (HaveMominHH==1 | HaveDadinHH==1))


gen PotentialDep = (Under19_LiveWithParent==1 | Under24FTS_LiveWithParent==1)
gen PotentialDep_LiveWithMom = (Under19_LiveWithMom==1 | Under24FTS_LiveWithMom==1)
gen PotentialDep_LiveWithDad = (Under19_LiveWithDad==1 | Under24FTS_LiveWithDad==1)

** Count up number of such kids per household per month. 
sort ssuid monthcode
by ssuid monthcode: egen N_PotentialDep = total(PotentialDep)
by ssuid monthcode: egen N_PotentialDep_LiveWithMom = total(PotentialDep_LiveWithMom)
by ssuid monthcode: egen N_PotentialDep_LiveWithDad = total(PotentialDep_LiveWithDad)


** Record info about age of youngest potential dep in the household
gen temp = tage
replace temp=. if PotentialDep~=1
sort ssuid monthcode
by ssuid monthcode: egen MinAge_PotentialDep = min(temp)
drop temp


** On the kid-under-19 records, record the person number of the person 
** who is the listed mom of that under-19 kid. 
gen epn_mom=epnmom if PotentialDep_LiveWithMom==1


** Count up number of distinct moms-of-under-19 kids
egen distinctmomtag = tag(ssuid monthcode epn_mom)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctMoms = total(distinctmomtag)


** At household-month level, record person numbers of up to 5 people
** identified as moms of an under-19 kid living with parent
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom1 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom1
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom2 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom2
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom3 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom3
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom4 = min(epn_mom)

replace epn_mom=. if epn_mom==temp_epn_mom4
sort ssuid monthcode epn_mom
by ssuid monthcode: egen temp_epn_mom5 = min(epn_mom)


** Generate an indicator, at the individual level, for being a mom of under-19 kid
gen PotentialMidGenMom=(pnum==temp_epn_mom1 | pnum==temp_epn_mom2 | pnum==temp_epn_mom3 | pnum==temp_epn_mom4 | pnum==temp_epn_mom4)


** Repeat for dads: count up number of distinct identified dads in household,
** at HH level record person numbers of up to 4 people identified as dads of 
** under-19 kids 
gen epn_dad=epndad if PotentialDep_LiveWithDad==1

egen distinctdadtag = tag(ssuid monthcode epn_dad)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctDads = total(distinctdadtag)

gen N_DistinctParents = N_DistinctMoms + N_DistinctDads


** At household-month level, record person numbers of up to 4 people
** identified as dads of an under-19 kid living with parent
sort ssuid monthcode epn_dad
by ssuid monthcode: egen temp_epn_dad1 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad1
sort ssuid monthcode epn_dad
by ssuid monthcode: egen temp_epn_dad2 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad2
sort ssuid monthcode epn_dad
by ssuid monthcode: egen temp_epn_dad3 = min(epn_dad)

replace epn_dad=. if epn_dad==temp_epn_dad3
sort ssuid monthcode epn_dad
by ssuid monthcode: egen temp_epn_dad4 = min(epn_dad)


** Generate an indicator, at the individual level, for being a dad of under-19 kid
gen PotentialMidGenDad=(pnum==temp_epn_dad1 | pnum==temp_epn_dad2 | pnum==temp_epn_dad3 | pnum==temp_epn_dad4)

gen PotentialMidGenParent=(PotentialMidGenMom==1 | PotentialMidGenDad==1)


** Drop intermediate variables that were used in the identification process
drop temp_epn_mom1 temp_epn_mom2 temp_epn_mom3 temp_epn_mom4 temp_epn_dad1 temp_epn_dad2 temp_epn_dad3 temp_epn_dad4



** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen ParentHasMom = (PotentialMidGenParent==1 & epnmom~=. )


** Count up how many potential-mid-gen-parents in the HH have their own moms in HH
sort ssuid monthcode
by ssuid monthcode: egen N_Parent_LiveWithMom = total(ParentHasMom)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed mom of that mid-gen parent 
gen epn_gmom=epnmom if ParentHasMom==1

** Count up number of distinct moms-of-parents
egen distinctgmomtag = tag(ssuid monthcode epn_gmom)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctGMom = total(distinctgmomtag)


** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid monthcode epn_gmom N_DistinctGMom
by ssuid monthcode: egen temp_epn_gmom1 = min(epn_gmom)

replace epn_gmom=. if epn_gmom==temp_epn_gmom1
sort ssuid monthcode epn_gmom
by ssuid monthcode: egen temp_epn_gmom2 = min(epn_gmom)



** Now identify if the PotentialMidGenParent has his/her own father in HH
gen ParentHasDad = (PotentialMidGenParent==1 & epndad~=. )

gen ParentHasParent = (ParentHasMom==1 | ParentHasDad==1)

** Count up how many potential-mid-gen-parents in the HH have their own dads in HH
sort ssuid monthcode
by ssuid monthcode: egen N_Parent_LiveWithDad = total(ParentHasDad)

** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed dad of that mid-gen parent 
gen epn_gdad=epndad if ParentHasDad==1

** Count up number of distinct parents-of-parents
egen distinctgdadtag = tag(ssuid monthcode epn_gdad)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctGDad = total(distinctgdadtag)

gen N_DistinctGP = N_DistinctGMom + N_DistinctGDad


** At household-month level, record person numbers of up to 2 people 
** identified as dads of a parent
sort ssuid monthcode epn_gdad
by ssuid monthcode: egen temp_epn_gdad1 = min(epn_gdad)

replace epn_gdad=. if epn_gdad==temp_epn_gdad1
sort ssuid monthcode epn_gdad
by ssuid monthcode: egen temp_epn_gdad2 = min(epn_gdad)


** Generate an indicator, at the individual level, for being a mom or dad of a parent (a grandparent)

gen PotentialOldGenParent=(pnum==temp_epn_gmom1 | pnum==temp_epn_gmom2 | pnum==temp_epn_gdad1 | pnum==temp_epn_gdad2)



** Identify spouses of these grandparents
gen temp_epn_spouse_gm1 = epnspous if pnum==temp_epn_gmom1
sort ssuid monthcode
by ssuid monthcode: egen epn_spouse_gm1 = min(temp_epn_spouse_gm1)
gen SpouseOfGM1=(pnum==epn_spouse_gm1)
replace PotentialOldGenParent=1 if SpouseOfGM1==1

gen temp_epn_spouse_gm2 = epnspous if pnum==temp_epn_gmom2
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_gm2 = min(temp_epn_spouse_gm2)
gen SpouseOfGM2=(pnum==epn_spouse_gm2)
replace PotentialOldGenParent=1 if SpouseOfGM2==1

gen temp_epn_spouse_gd1 = epnspous if pnum==temp_epn_gdad1
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_gd1 = min(temp_epn_spouse_gd1)
gen SpouseOfGD1=(pnum==epn_spouse_gd1)
replace PotentialOldGenParent=1 if SpouseOfGD1==1

gen temp_epn_spouse_gd2 = epnspous if pnum==temp_epn_gdad2
sort ssuid monthcode
by ssuid monthcode: egen epn_spouse_gd2 = min(temp_epn_spouse_gd2)
gen SpouseOfGD2=(pnum==epn_spouse_gd2)
replace PotentialOldGenParent=1 if SpouseOfGD2==1

gen PotentialOldGenMom = ((pnum==temp_epn_gmom1 | pnum==temp_epn_gmom2)  & esex==2)


** Make other demographic variables

gen Employed = (rmesr==1 | rmesr==2 | rmesr==3 | rmesr==4 | rmesr==5)

** Note that education coding is a bit different in 2014 than in earlier years
gen HSGrad = (eeduc==39 )
gen SomeCollege = (eeduc==40 | eeduc==41 | eeduc==42)
gen CollegeGrad = (eeduc==43 | eeduc==44 | eeduc==45 | eeduc==46 )

gen Female = (esex==2)

gen White = (erace==1)

gen Married = (ems==1 | ems==2)

gen FTStudent = (renroll==1 | eedftpt==1)

save TempData, replace


*******************************************************************************
** Keep and save just the data on mid-gen parents WHO ARE NOT ALSO POTENTIAL
** OLD GENERATION PARENTS
keep if PotentialMidGenParent==1 & PotentialOldGenParent~=1

keep ssuid swave monthcode pnum PotentialMidGenParent PotentialMidGenMom tage Employed HSGrad SomeCollege CollegeGrad Female White Married ParentHasParent N_DistinctParents N_DistinctGP MinAge_PotentialDep  wpfinwgt erelrp tehc_st tpearn thtotinc FTStudent PotentialDep
destring tehc_st, gen(tfipsst)
duplicates drop

save MidGenParentData2014_w4, replace
clear


*******************************************************************************
** Keep and save just the data on old-gen parents
use TempData
**keep if PotentialOldGenParent==1 & PotentialMidGenParent~=1
keep if PotentialOldGenParent==1

keep ssuid swave monthcode pnum PotentialOldGenParent PotentialOldGenMom PotentialMidGenParent tage Employed HSGrad SomeCollege CollegeGrad Female White Married N_DistinctGP N_PotentialDep MinAge_PotentialDep wpfinwgt erelrp tehc_st tpearn thtotinc 
destring tehc_st, gen(tfipsst)
duplicates drop

save OldGenParentData2014_w4, replace
clear



********************************************************************************
** Identify cases of adults who do NOT have potential child 
** deps living with them, but who do have "elderly" parents living with them.


use TempData
gen PotentialEmptyNester = (PotentialDep==0 & PotentialMidGenParent==0 & tage>24)

** Now identify if the PotentialMidGenParent has his/her own mother in HH
gen EmptyNesterHasMom = (PotentialEmptyNester==1 & epnmom~=. )


** On the potential-mid-gen-EN-parents records, record the person number of the person 
** who is the listed mom of that mid-gen empty nester parent 
gen epn_engmom=epnmom if EmptyNesterHasMom==1


** Count up number of distinct moms-of-parents
egen distinctengmomtag = tag(ssuid epn_engmom)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctENGMom = total(distinctengmomtag)



** At household-month level, record person numbers of up to 2 people 
** identified as moms of a parent
sort ssuid monthcode epn_engmom
by ssuid monthcode: egen temp_epn_engmom1 = min(epn_engmom)

replace epn_engmom=. if epn_engmom==temp_epn_gmom1
sort ssuid monthcode epn_engmom
by ssuid monthcode: egen temp_epn_engmom2 = min(epn_engmom)


** Now identify if the PotentialMidGenENParent has his/her own father in HH
gen EmptyNesterHasDad = (PotentialEmptyNester==1 & epndad~=. )

gen EmptyNesterHasParent = (EmptyNesterHasMom==1 | EmptyNesterHasDad==1)


** On the potential-mid-gen-parents records, record the person number of the person 
** who is the listed dad of that mid-gen parent 
gen epn_engdad=epndad if EmptyNesterHasDad==1


** Count up number of distinct parents-of-parents
egen distinctengdadtag = tag(ssuid monthcode epn_engdad)
sort ssuid monthcode
by ssuid monthcode: egen N_DistinctENGDad = total(distinctengdadtag)

gen N_DistinctENGP = N_DistinctENGMom + N_DistinctENGDad

** At household-month level, record person numbers of up to 2 people 
** identified as dads of a parent
sort ssuid monthcode epn_engdad
by ssuid monthcode: egen temp_epn_engdad1 = min(epn_engdad)

replace epn_engdad=. if epn_gdad==temp_epn_engdad1
sort ssuid monthcode epn_engdad
by ssuid monthcode: egen temp_epn_engdad2 = min(epn_engdad)



** Generate an indicator, at the individual level, for being a parent of a potential empty nester
** And then generate one for being a female empty nest grandparent

gen PotentialEmptyNestGP=(pnum==temp_epn_engmom1 | pnum==temp_epn_engmom2 | pnum==temp_epn_engdad1 | pnum==temp_epn_engdad2)

** Identify spouses of these grandparents
gen temp_epn_spouse_engm1 = epnspous if pnum==temp_epn_engmom1
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_engm1 = min(temp_epn_spouse_engm1)
gen SpouseOfENGM1=(pnum==epn_spouse_engm1)
replace PotentialEmptyNestGP=1 if SpouseOfENGM1==1

gen temp_epn_spouse_engm2 = epnspous if pnum==temp_epn_engmom2
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_engm2 = min(temp_epn_spouse_engm2)
gen SpouseOfENGM2=(pnum==epn_spouse_engm2)
replace PotentialEmptyNestGP=1 if SpouseOfENGM2==1

gen temp_epn_spouse_engd1 = epnspous if pnum==temp_epn_engdad1
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_engd1 = min(temp_epn_spouse_engd1)
gen SpouseOfENGD1=(pnum==epn_spouse_engd1)
replace PotentialEmptyNestGP=1 if SpouseOfENGD1==1

gen temp_epn_spouse_engd2 = epnspous if pnum==temp_epn_engdad2
sort ssuid monthcode 
by ssuid monthcode: egen epn_spouse_engd2 = min(temp_epn_spouse_engd2)
gen SpouseOfENGD2=(pnum==epn_spouse_engd2)
replace PotentialEmptyNestGP=1 if SpouseOfENGD2==1

gen PotentialEmptyNestGP_Female = (PotentialEmptyNestGP==1 & esex==2)


save Temp2Data, replace




******************************************************************
** Keep data on potential empty-nester parents
keep if PotentialEmptyNester==1 & PotentialEmptyNestGP~=1

destring tehc_st, gen(tfipsst)

keep ssuid monthcode pnum PotentialEmptyNester tage Employed HSGrad SomeCollege CollegeGrad Female White Married EmptyNesterHasParent N_DistinctENGP  wpfinwgt erelrp tfipsst tpearn thtotinc 

duplicates drop

save EmptyNesterData2014_w4, replace
clear




*******************************************************************************
** Keep and save just the data on potential grandparents in households where
** mid-gen parent is empty-nester
use Temp2Data
destring tehc_st, gen(tfipsst)


keep if PotentialEmptyNestGP==1

keep ssuid monthcode pnum PotentialEmptyNestGP PotentialEmptyNestGP_Female tage Employed HSGrad SomeCollege CollegeGrad Female White Married   wpfinwgt erelrp tfipsst tpearn thtotinc 

duplicates drop

save EmptyNesterGPData2014_w4, replace
clear

